﻿using shp2postgres.log;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;

namespace shp2postgres.sql
{
    public partial class SQLForm : Form
    {
        public bool connectioned = false;
        public DabaBaseType DBType { get; set; } = 0;

        public SQLTools sqlTools { get; set; }


        public SQLForm(DabaBaseType dbType, SQLTools sqlTools)
        {
            InitializeComponent();
            this.DBType = dbType;
            this.sqlTools = sqlTools;
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void connBtn_Click(object sender, EventArgs e)
        {
            if (this.serverURL == null || this.serverURL.TextLength == 0)
            {
                this.serverURL.Focus();
                this.serverURL.Text = "请输入正确的数据库连接地址";
                return;
            }

            if (this.dbName == null || this.dbName.TextLength == 0)
            {
                this.dbName.Focus();
                this.dbName.Text = "请输入正确的数据库名称";
                return;
            }

            if (this.userName == null || this.userName.TextLength == 0)
            {
                userName.Focus();
                return;
            }
            if (this.password == null || this.password.TextLength == 0)
            {
                password.Focus();
                return;
            }

            // 
            //Log.InfoAsync("shujklian");
            if (null == sqlTools)
            {
                Log.InfoAsync("sqltools为空");
                return;
            }
            connectioned = sqlTools.ConnectSql(DBType, serverURL.Text, dbName.Text, userName.Text, password.Text);
            if (connectioned)
            {
                MessageBox.Show("数据库连接成功！");
            } else
            {
                MessageBox.Show("数据库连接失败！");
            }
            this.Close();
        }
        

    }
}
